package com.e3.web.widget.client;

import java.util.List;

import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.google.gwt.user.client.Element;


public class MyListField<T> extends ComboBox<ListTypeModel<T>> implements MyField<ListTypeModel<T>> {
	
	private List<ListTypeModel<T>> listValues;
	
	private ListStore<ListTypeModel<T>> store = new ListStore<ListTypeModel<T>>();
	
	private boolean isHide = false;

	public MyListField() {
		super();
	}
	
	public MyListField(List<ListTypeModel<T>> listValues) {
		super();
		this.listValues = listValues;
		
	}
	
	public void setListValues(List<ListTypeModel<T>> listValues){
		this.listValues = listValues;
		init();
	}
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
	}

	private void init(){
		if(listValues == null){
			return;
		}
		
		store.add(listValues);
		setStore(store);
	    setDisplayField(ListTypeModel.NAME);
	    setValueField(ListTypeModel.VALUE);
	    setTriggerAction(TriggerAction.ALL);
	}
	
	public ListTypeModel<T> getFieldValue() {
		return getValue();
	}

	@SuppressWarnings("unchecked")
	public void setFieldValue(Object value) {
		setSelectValue((T)value);
	}
	
	public void setSelectValue(T value){
		List<ListTypeModel<T>> models = this.getStore().getModels();
		for (ListTypeModel<T> model : models) {
			if(model.getValue().equals(value)){
				setValue(model);
				break;
			}
		}
	}
	
	@Override
	public void hide() {
		isHide = true;
		super.hide();
	}

	public boolean isHide() {
		return isHide;
	}
}
